﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using Newtonsoft.Json;

namespace WCS.API.Filters
{
    public class LogFilter : ActionFilterAttribute
    {
        private readonly ILogger<LogFilter> logger;

        public LogFilter(ILogger<LogFilter> logger)
        {
            this.logger = logger;
        }


        public override void OnActionExecuted(ActionExecutedContext context)
        {
            if(context != null)
            {
                var desc = context.ActionDescriptor as ControllerActionDescriptor;
                var result = context.Result as ObjectResult;
                logger.LogInformation($"The  controller{desc?.ControllerName} excute the method {desc?.ActionName},reponse the rms message  is:{JsonConvert.SerializeObject(result?.Value)}");
            }
        }

        public override void OnActionExecuting(ActionExecutingContext context)
        {
            var desc = context.ActionDescriptor as ControllerActionDescriptor;
            var arg = context.ActionArguments.Where(x => !x.Key.Contains("Repository"));
            logger.LogInformation($"The controller {desc?.ControllerName} excute the method {desc?.ActionName},receive rms message is:{JsonConvert.SerializeObject(arg)}");
        }
    }
}
